package com.higgins.client.components;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Widget;
import com.higgins.client.GreetingService;
import com.higgins.client.GreetingServiceAsync;
import com.higgins.client.dataModel.CoveringLetterDTO;
import com.higgins.client.dataModel.StrategyDTO;
import com.higgins.client.generic.components.GenericTable;

public class CoveringLetterTable extends GenericTable	{
	private static final Logger log = Logger.getLogger(CoveringLetterTable.class.getName());
	private final GreetingServiceAsync greetingService = GWT
			.create(GreetingService.class);
	
	public CoveringLetterTable(CoveringLetterColumns columns)	{
		super(columns);
		retrieveDTOs();
	}
	
	private List<List<Object>> convertDTOtoRowArray(List<CoveringLetterDTO> coveringLetterDTOs)	{
		List<List<Object>> rows = new ArrayList<List<Object>>();
		for (CoveringLetterDTO coveringLetterDTO:coveringLetterDTOs)	{
			List<Object> row = new ArrayList<Object>();
			row.add(coveringLetterDTO.getId());
			row.add(coveringLetterDTO.getName());
			row.add(coveringLetterDTO.getGoogleCode());
			rows.add(row);
		}
		return rows;
	}
	
	public void updateItemCell(int row)	{
		CoveringLetterDTO coveringLetterDTO = new CoveringLetterDTO();
		coveringLetterDTO.setId(getId(row));
		coveringLetterDTO.setName(getText(row, 1));
		coveringLetterDTO.setGoogleCode(getText(row, 2));
		updateRow(coveringLetterDTO);
	}
	
	protected void retrieveDTOs()	{
		try {
			greetingService.retrieveCoveringLetters(new AsyncCallback<List<CoveringLetterDTO>>()	{
					@Override
					public void onFailure(Throwable caught) {
						log.info("CoveringLetterDTO: " + caught.toString());
					}

					@Override
					public void onSuccess(List<CoveringLetterDTO> result) {
						loadData(convertDTOtoRowArray(result));
					}
				});
		} catch (IllegalArgumentException e) {
			log.info("CoveringLetterDTO: " + e.toString());
		} /*catch (SecurityAccessDeniedException e) {
			log.info("SecurityAccessDeniedException: " + e.toString());
		} */
	}	
	private void updateRow(CoveringLetterDTO coveringLetterDTO)	{
		try {
			greetingService.updateCoveringLetter(coveringLetterDTO, new AsyncCallback<String>()	{
					@Override
					public void onFailure(Throwable caught) {
						log.info("CoveringLetterDTO: " + caught.toString());
					}

					@Override
					public void onSuccess(String result) {
						setRowIdAfterUpdate(result);
					}
				});
		} catch (IllegalArgumentException e) {
			log.info("CoveringLetterDTO: " + e.toString());
		} /*catch (SecurityAccessDeniedException e) {
			log.info("SecurityAccessDeniedException: " + e.toString());
		} 		*/
	}
}